package com.pattern.prototype;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 浅克隆 实现Cloneable接口 重写clone方法
 * @author LYao15
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class PrototypePattern implements Cloneable{
    private User user;
    private int age;

    public PrototypePattern(User user){
        this.user = user;
    }

    /**
     * 实现clone()方法实现浅克隆
     * @return
     */
    @Override
    public PrototypePattern clone() {
        //需要实现cloneable的接口，直接继承object就好，它里面自带一个clone方法！
        Object obj = null;
        try {
            obj = super.clone();
            return (PrototypePattern)obj;
        } catch (CloneNotSupportedException e) {
            System.out.println("不支持克隆方法！");
            return null;
        }
    }
}
